#!/system/bin/sh

# This script is supposed to be used with Another Term Lite.
# See https://github.com/green-green-avk/AnotherTermLite/wiki
# for `DATA_DIR', `LIB_DIR' and `TERMSH_UID' environment variables meaning.

BASE_DIR="${0%/*}" # Old Androids could not have `dirname'.

export TERMSH_UID="$USER_ID" # "$(id -u)" does not work for old Androids.

export PROOT_TMP_DIR="$BASE_DIR/tmp"

export PATH=/bin:/usr/bin
export USER=my_acct
export HOME="/home/$USER"
SHELL=/bin/bash
FB_SHELL=/bin/sh

if [ -n "$1" ]
then
	UG="--change-id=$1"
	if [ "$1" = '0:0' ]
	then
		UG=-0
		export PATH=/sbin:/usr/sbin:/bin:/usr/bin
		export USER=root
		export HOME=/root
	fi
else UG=
fi

# Optional mappings
MAP_ANDROID_DATA_DIR='-b /data'
# ===

if [ ! -x "$BASE_DIR/root/$SHELL" ]
then
SHELL="$FB_SHELL"
fi

unset TMPDIR
unset LD_LIBRARY_PATH

export TERMSH=termsh # Just for convenience.

"$DATA_DIR/root/bin/proot" -r "$BASE_DIR/root" $UG \
-b /dev -b /proc -b /sys -b /system -b /storage \
$MAP_ANDROID_DATA_DIR \
-b "$LIB_DIR/libtermsh.so":/bin/termsh \
--link2symlink -p -L --tcsetsf2tcsets \
-w "$HOME" "$SHELL" -l
